package com.exploreca.tourfinder;


import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.List;
import android.app.ListActivity;
import android.widget.ArrayAdapter;

import com.exploreca.tourfinder.db.FriendsDataSource;

public class MainActivity extends ListActivity {
	
	public static final String LOGCAT = "exploreca";
	
	FriendsDataSource datasource;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		try {
			setContentView(R.layout.activity_main);	
		}
		catch(Exception e) {
			Log.i(LOGCAT, e.toString());	
		}
			
		datasource = new FriendsDataSource(this);
		
	    setContactsList();
	}

	private void setContactsList() {
		datasource.openDB();
	    
	    List<Contact> values = datasource.getAllContacts();

	    ArrayAdapter<Contact> adapter = 
	    		new ArrayAdapter<Contact>(this, 
	    				android.R.layout.simple_list_item_1, 
	    				values);
	    setListAdapter(adapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public void addUser(View v) {
		String username = getUsername();
		String phone = getPhone();
		TextView tv = (TextView) this.findViewById(R.id.textView1);
		
		if (datasource.insert(username, phone)) {
			tv.setText("Contact succesfully added!");	
			
			ArrayAdapter<Contact> adapter = (ArrayAdapter<Contact>) getListAdapter();
		    
		    adapter.add(new Contact(username, phone));
		    
		    adapter.notifyDataSetChanged();
		    
		} else {
			tv.setText("Contact already exists!");
		}			    
	}
	
	public void checkUser(View v) {
		String username = getUsername();
		TextView tv = (TextView) this.findViewById(R.id.textView1);
		
		if (datasource.check(username)) {
			tv.setText("Contact already exists!");	
		} else {
			tv.setText("Contact doesn't exist!");
		}		
	}
	
	public void deleteUser(View v) {
		String username = getUsername();
		TextView tv = (TextView) this.findViewById(R.id.textView1);
		
		if (datasource.delete(username)) {
			ArrayAdapter<Contact> adapter = 
					(ArrayAdapter<Contact>) getListAdapter();
			tv.setText("Contact succesfully deleted!");
			for (int i = 0; i < adapter.getCount(); ++i)
		    	if (adapter.getItem(i).getName().equals(username)) {
		    		adapter.remove(adapter.getItem(i));
		    		break;
		    	}
			adapter.notifyDataSetChanged();
		} else {
			tv.setText("Contact doesn't exist!");
		}
	}
	
	private String getUsername() {
		return ((EditText) this.findViewById(R.id.editText1)).getText().toString();
	}
	
	private String getPhone() {
		return ((EditText) this.findViewById(R.id.editText2)).getText().toString();
	}

	@Override
	protected void onResume() {
		super.onResume();
		datasource.openDB();
	}
	
	@Override 
	protected void onPause() {
		super.onPause();
		datasource.closeDB();
	}
	
}
